package org.pub.fast.jdbc.cross.util;


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @nopublish
 * @author hey
 * 
 */
public class SyncMapDecorator implements Map {

	Map map = null;

	ReadWriteLock lock;

	public SyncMapDecorator(Map map) {
		this.map = map;
		lock = new ReadWriteLock();
	}

	public void clear() {
		lock.writeLock();
		try {
			map.clear();
		} finally {
			lock.writeUnLock();
		}

	}

	public boolean containsKey(Object key) {
		lock.readLock();

		try {
			return map.containsKey(key);
		} finally {
			lock.readUnLock();
		}

	}

	public boolean containsValue(Object value) {

		lock.readLock();
		try {
			return map.containsValue(value);
		} finally {
			lock.readUnLock();
		}

	}

	public Set entrySet() {
		return map.entrySet();
	}

	public boolean equals(Object o) {
		return map.equals(o);
	}

	public Object get(Object key) {
		lock.readLock();
		try {
			return map.get(key);
		} finally {
			lock.readUnLock();
		}

	}

	public int hashCode() {
		return map.hashCode();
	}

	public boolean isEmpty() {
		lock.readLock();
		try {
			return map.isEmpty();
		} finally {
			lock.readUnLock();
		}
	}

	public Set keySet() {
		lock.readLock();
		try {
			return map.keySet();
		} finally {
			lock.readUnLock();
		}
	}

	public Object put(Object key, Object value) {
		lock.writeLock();
		try {
			return map.put(key, value);
		} finally {
			lock.writeUnLock();
		}

	}

	public void putAll(Map t) {
		lock.writeLock();
		try {
			map.putAll(t);
		} finally {
			lock.writeUnLock();
		}

	}

	public Object remove(Object key) {
		lock.writeLock();
		try {
			return map.remove(key);
		} finally {
			lock.writeUnLock();
		}

	}

	public int size() {
		lock.readLock();
		try {
			return map.size();
		} finally {
			lock.readUnLock();
		}
	}

	public Collection values() {
		lock.readLock();
		try {
			return map.values();
		} finally {
			lock.readUnLock();
		}
	}

	public static void main(String args[]) {

		Map map = new SyncMapDecorator(new HashMap());
		map.put("asdf", "asdfasdf");
		System.out.println(map.size());
	}

}

